﻿#include "stdafx.h"
#include "qg/qgasv.h"
#include "qg/qgrdraw.h"

//////////////////////////////////////////////////////////////////////////
// 스크라이브
KIMPL_SUBCLASS_ABSTRACT(qgScribe, "Scribe");

//
qgScribe::qgScribe()
{
	_wids = new WidArray();
}

//
qgScribe::~qgScribe()
{
	delete _wids;
}

//
bool qgScribe::PointToX(kint nth, kbool istrail, kint* xloc)
{
	k_return_value_if_fail(xloc, false);
	k_return_value_if_fail(nth >= 0, false);

	kint wc = 0;
	kint cnt = _wids->Count();

	for (kint i = 0; i < cnt; i++)
	{
		if (i == nth && !istrail)
		{
			*xloc = wc;
			return true;
		}

		wc += (*_wids)[i];

		if (i == nth && istrail)
		{
			*xloc = wc;
			return true;
		}
	}

	*xloc = wc;

	return false;
}

//
bool qgScribe::XToPoint(kint xloc, kint* nth, kbool* trail)
{
	k_return_value_if_fail(nth, false);
	k_return_value_if_fail(trail, false);

	if (xloc < 0)
	{
		*nth = -1;
		*trail = true;
	}
	else
	{
		kint wd = 0;
		kint cnt = _wids->Count();

		for (kint i = 0; i < cnt; i++)
		{
			kint gw = (*_wids)[i];

			if (xloc<(wd + gw))
			{
				*nth = i;
				*trail = (xloc - wd) >(gw / 2) ? true : false;
				return true;
			}

			wd += gw;
		}

		*nth = cnt;
		*trail = false;
	}

	return true;
}


//////////////////////////////////////////////////////////////////////////
// 폰트 베이스
KIMPL_SUBCLASS_ABSTRACT(qgFont, "Font");

//
qgFont::qgFont()
: super_type()
, _color(255, 255, 255)
, _size(16), _fmt(0)
{
	_BaseRoam(QGROAM_EVENT);
}

//
void qgFont::SetSize(kint size)
{
	_size = size;
}

//
void qgFont::Write(kint x, kint y, kint flags, const char* str)
{
	tpl::Rect rt(x, y, K_MAX_USHORT, K_MAX_USHORT);
	TextDraw(rt, flags, str);
}

//
void qgFont::WriteFormat(kint x, kint y, kint flags, const char* str, ...)
{
	int ret;
	va_list va;
	kchar* buf;

	va_start(va, str);
	ret = k_vsnprintf(NULL, 0, str, va);
	va_end(va);

	buf = (char*)alloca(ret + 1);
	va_start(va, str);
	k_vsnprintf(buf, ret + 1, str, va);
	va_end(va);

	tpl::Rect rt(x, y, K_MAX_USHORT, K_MAX_USHORT);
	TextDraw(rt, flags, buf);
}

//
void qgFont::WriteL(kint x, kint y, kint flags, const kwchar* uni)
{
	tpl::Rect rt(x, y, K_MAX_USHORT, K_MAX_USHORT);
	TextDrawL(rt, flags, uni);
}

//
void qgFont::WriteFormatL(kint x, kint y, kint flags, const kwchar* uni, ...)
{
	int ret;
	va_list va;
	kwchar* buf;

	va_start(va, uni);
	ret = k_vsnwprintf(NULL, 0, uni, va);
	va_end(va);

	buf = (kwchar*)alloca((ret + 1) * sizeof(kwchar));
	va_start(va, uni);
	k_vsnwprintf(buf, ret + 1, uni, va);
	va_end(va);

	tpl::Rect rt(x, y, K_MAX_USHORT, K_MAX_USHORT);
	TextDrawL(rt, flags, buf);
}

